package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.TeacherService;

import java.util.Scanner;

public abstract class BaseTeacherController {
    private TeacherService teacherService = new TeacherService();
    public Scanner sc = new Scanner(System.in);

    public void start() {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addTeacher();
                    break;
                case "2":
                    deletTeacher();
                    break;
                case "3":
                    updateTeacher();
                    break;
                case "4":
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("感谢使用老师管理系统,再见!");
                    return;
                default:
                    System.out.println("输入有误!请重新输入:");
                    break;
            }
        }

    }

    //添加老师
    public void addTeacher() {
        //获取录入的学生id,false表示不存在,可以添加
        String id = inputTeacherId(false);
        //获取键盘录入的老师信息
        Teacher teacher = getTeacher(id);
        boolean flag = teacherService.addTeacher(teacher);
        if (flag) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    //删除老师
    public void deletTeacher() {
        //通过业务员方法拿到信息返还到数组
        Teacher[] teachers = teacherService.findAllTeacher();
        //如果为null,表示数组没有结果
        if (teachers == null) {
            System.out.println("系统暂无信息,请添加后再删除");
            return;
        }
        String id = inputTeacherId(true);
        teacherService.deletTeachuer(id);
        System.out.println("删除成功");

    }

    //修改老师
    public void updateTeacher() {
        //通过业务员方法拿到信息返还到数组
        Teacher[] teachers = teacherService.findAllTeacher();
        //如果为null,表示数组没有结果
        if (teachers == null) {
            System.out.println("系统暂无信息,请添加后再修改");
            return;
        }
        //获取键盘录入的要修改的id,true表示存在
        String updateId = inputTeacherId(true);
        //获取键盘录入的老师信息
        Teacher teacher = getTeacher(updateId);
        //调用业务员的修改方法判断
        teacherService.updateTeacher(updateId, teacher);

        System.out.println("修改成功");

    }

    //查看老师
    public void findAllTeacher() {
        //通过业务员方法拿到信息返还到数组
        Teacher[] teachers = teacherService.findAllTeacher();
        //如果为null,表示数组没有结果
        if (teachers == null) {
            System.out.println("系统暂无信息,请添加后再查看");
            return;
        }
        System.out.println("工号\t\t姓名\t年龄\t生日");
        //有结果就遍历数组取值
        for (int i = 0; i < teachers.length; i++) {
            Teacher ter = teachers[i];
            //打印不为null的对象
            if (ter != null) {
                System.out.println(ter.getSid() + "\t" + ter.getName() + "\t" + ter.getAge() + "\t\t" + ter.getBirthday());
            }
        }
    }

    //获取用户录入的id
    public String inputTeacherId(boolean flag) {
        String id;
        while (true) {
            System.out.println("请输入工号:");
            id = sc.next();
            //调用业务员类的方法判断工号是否存在
            boolean exists = teacherService.isExists(id);
            if (exists == flag) {
                break;
            } else {
                System.out.println("工号不存在!请重新输入:");
            }
        }
        return id;
    }

    //获取录入老师信息
    public abstract Teacher getTeacher(String id);
}
